<?php

namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ProductController extends Controller
{

    public function  index(Request $request)
    {
        $query = DB::table('product');
        if ($request->input('providerId')) {
            $query = $query->where('ti_provider',  $request->input('providerId'));
        }
        if ($request->input('zoneName')) {
            $query = $query->where('vc_zone_name',  $request->input('zoneName'));
        }
        if ($request->input('onlyName')) {
            $list = $query->select('vc_zone_name')->distinct('vc_zone_name')->get();
            return response([
                'success' => true,
                'total'   => count($list),
                'items'   => $list
            ]);
        } else {
            $total = $query->count();
            $list = $query->orderBy('i_valid_days','asc')->get();
            return response([
                'success' => true,
                'total'   => $total,
                'items'   => $list
            ]);
        }
    }

    public function  getList($provider,$zoneIds,$esim)
    {
        $query = DB::table('tbl_bill_open_product');

        if ($provider!='all') {
            $query = $query->whereRaw("FIND_IN_SET($provider,vc_provider_list)");
        }
        foreach ($zoneIds as $zoneId){
            $query = $query->whereRaw("FIND_IN_SET($zoneId,vc_zone_id)");
        }
        if($esim){
            $query = $query->where('ti_esim',  1);
            $query = $query->where('ti_esim_on_sale',  1);
        } else {
//            $query = $query->where('ti_esim',  0);
            $query = $query->where('ti_enable_top_up',  1);
        }
        $total = $query->count();
        $lists = $query->orderBy('i_valid_days','asc')->get();
        $zones = array();
        foreach ($lists as $key=> $list){
            $zone_str = '';
            if($list->vc_zone_id){
                $res = explode( ',', $list->vc_zone_id);
                foreach ($res as $v){
                    $zones[] = $v;
                }
                $product_zones = DB::table('tbl_bill_open_zone')->whereRaw("FIND_IN_SET('SCT',vc_platform_list)")->whereIn('i_zone_id',$res)->orderBy('ti_hot_zone','desc')->orderBy(DB::raw('convert(`vc_name` using gbk)'))->get(['vc_name'])->toArray();
                foreach ($product_zones as $zone_name){
                    $zone_str .= $zone_name->vc_name.'、';
                }
            }
            $lists[$key]->product_zones = trim($zone_str,'、');
        }
        $zones = array_unique($zones);
        $zonesList = DB::table('tbl_bill_open_zone')->whereRaw("FIND_IN_SET('SCT',vc_platform_list)")->whereIn('i_zone_id',$zones)->orderBy('ti_hot_zone','desc')->orderBy(DB::raw('convert(`vc_name` using gbk)'))->get()->toArray();
        return response([
            'success' => true,
            'total'   => $total,
            'items'   => $lists,
            'zones_list'=>$zonesList
        ]);

    }
}